<?php
function echoFixtureRow($fixture, $canEdit, $rounds) {
    echo getEchoTR($fixture->round, "center");
    $name = $fixture->round;
    if (isset ($rounds) && isset ($rounds[$fixture->round])) {
        $round = $rounds[$fixture->round];
        $name = $round->name;
    }
    echo "  <td>$name</td>\n";
    echo "  <td>" . getEchoValue($fixture->homeTeam->getRosterLink()) . "<br />(" . getEchoValue($fixture->homeTeam->coach->getName()) . ")</td>\n";
    echo "  <td>vs</td>\n";
    echo "  <td>" . getEchoValue($fixture->awayTeam->getRosterLink()) . "<br />(" . getEchoValue($fixture->awayTeam->coach->getName()) . ")</td>\n";
    if ($canEdit) {
        echo "  <td><a href=\"?remove=" . $fixture->id . "\">Remove</a></td>";
    } else {
        echo "  <td>&nbsp;</td>\n";
    }
    echo "</tr>\n";
}

function echoRoundSelectArray($name, $array) {
    echo "<select name=\"" . $name . "\" >\n";
    foreach (array_keys($array) as $key) {
        $value = $array[$key];
        echo "<option value=\"" . $key . "\"";
        echo ">" . $value->name . "</option>\n";
    }
    echo "</select></td>\n";
}

include_once ("includes/init.php");
$title = $season->season . " fixtures";
include_once ("includes/header.php");
$frd = new FixtureRoundDao();
$rounds = $frd->findFixtureRounds($season);
$image = $season->getImage();

echo<<< EOQ
	<caption><h1>$season->season Scheduled Fixtures $image</h1></caption>
EOQ;

$canEdit = isCommissioner();

if ($canEdit) {
    $remove = getParam("remove");
    $action = getParam("action");
    $round = getParam("round");
    $name = getParam("name");
    $home_team = getParam("home_team");
    $away_team = getParam("away_team");
    if (isset ($remove)) {
        if (deleteFixture($remove)) {
            echo<<< EOQ
	<tr>
		<td align="center">Fixture removed</td>
	</tr>
EOQ;
        } else {
            echo<<< EOQ
	<tr>
		<td align="center">Unable to remove fixture</td>
	</tr>
EOQ;
        }
    } else
        if (isset ($action)) {
            if (0 == strcmp("auto", $action)) {
            	$fg = new FixtureGenerator();
            	$fg->generatePlayEveryConferenceOnce();
            } else
                if (0 == strcmp("round", $action)) {
                    $num = sizeof($rounds) + 1;
                    $fixRound = new FixtureRound();
                    $fixRound->init($season->id, $num, $name);
                    array_push($rounds, $fixRound);
                    $frd->save($rounds);
                    echo<<< EOQ
	<tr>
		<td align="center">Round $name added</td>
	</tr>
EOQ;
                } else
                    if (addFixture($season, $round, $home_team, $away_team)) {
                        echo<<< EOQ
	<tr>
		<td align="center">Fixture added</td>
	</tr>
EOQ;
                    } else {
                        echo<<< EOQ
	<tr>
		<td align="center">Unable to add fixture</td>
	</tr>
EOQ;
                    }
        }
}

$fixtures = loadFixtures($season, $teams);
echo<<< EOQ
	<tr>
		<td align="center">
			<table border="0" cellPadding="5" cellSpacing="0" width="100%">
				<tr>
					<th>Round</th>
					<th>Home</th>
					<th>&nbsp;</th>
					<th>Away</th>
					<th>&nbsp;</th>
				</tr>
EOQ;
if (sizeof($fixtures) == 0) {
    echo<<< EOQ
				<tr>
					<td colspan="5">No scheduled games</td>
				</tr>
EOQ;
}
foreach ($fixtures as $fixture) {
    echoFixtureRow($fixture, $canEdit, $rounds);
}
echo<<< EOQ
			</table>
		</td>
	</tr>
EOQ;
if ($canEdit) {
    echo<<< EOQ
	<tr>
		<td align="center">
			<form action="fixtures.php" method="POST">
			<input type="hidden" name="action" value="auto" />
			<input type="submit" name="submit" value="Auto Fixtures" title="Automatically create a fixture list" />
			<br /><span class="sup">This automatically creates a random fixture list with each team playing every other team in the same conference once</span></td>
			</form>
		<td>
	</tr>

EOQ;
    if (count($rounds) > 0) {
        echo<<< EOQ
	<tr>
		<td align="center">
			<form action="fixtures.php" method="POST">
			<input type="hidden" name="action" value="add" />
			<table border="0" cellPadding="5" cellSpacing="0" width="100%">
				<tr>
					<th>Round</th>
					<th>Home</th>
					<th>Away</th>
				</tr>
				<tr>
					<td>
EOQ;
        echoRoundSelectArray("round", $rounds);
        echoTeamSelect("home_team", $teams, $home_team, 0);
        echoTeamSelect("away_team", $teams, $away_team, 0);
        echo<<< EOQ
				</tr>
				<tr>
					<th colspan="3"><input type="submit" name="submit" value="Add Fixture"></th>
				</tr>
			</table>
			</form>
		</td>
	</tr>
EOQ;
    }
    echo<<< EOQ
	<tr>
		<td>
			<form action="fixtures.php" method="POST">
			<input type="hidden" name="action" value="round" />
			<b>Name:</b> <input type="text" name="name" maxlength="64" size="32" />	
			<input type="submit" name="submit" value="Add Round" /></td>
			</form>
		<td>
	</tr>

EOQ;
}
include ("includes/footer.php");
?>
